/**
 * 深迪半导体(http://www.senodia.com/) SH2100 G-Sensor 驱动
 * Copyright (c) 2021 深圳市智辰科技有限公司
 * All rights reserved.
 */

#ifndef _DRIVER_SH2100_H_
#define _DRIVER_SH2100_H_

#include "nonos.h"

// I2C 地址(bit7~1)：当 SDO 为低时为 1101100x；当 SDO 为高时为 1101101x
// 地址读写位(bit0)：0-写，1-读
// ------+----------------+----------------
//       |    SDO = 0     |    SDO = 1
// ------+----------------+----------------
// WRITE | 0xD8(11011000) | 0xDA(11011010)
// ------+----------------+----------------
// READ  | 0xD9(11011001) | 0xDB(11011011)
// ------+----------------+----------------
#define SH2100_I2C_ADDR_WRITE(sdo)	(sdo ? 0xDA : 0xD8)
#define SH2100_I2C_ADDR_READ(sdo)	(sdo ? 0xDB : 0xD9)

// SENB - I2C/SPI(CSN) Protocol select: 
// 1: SPI idle mode/I2C communication enabled; 
// 0: SPI communication mode/I2C disabled

// 接口驱动数据结构
typedef struct {
	// GPIO 驱动函数
	FUNC_GPIO_Out senb;    	// SPI-CSN   / I2C-NULL(必须置空)
	FUNC_GPIO_Out sck;   	// SPI-SCLK  / I2C-CLK
	FUNC_GPIO_Out sda;   	// SPI-MOSI  / I2C-SDA-BidiOut
	FUNC_GPIO_ReadIn sdo;	// SPI-MISO  / I2C-SDA-BidiIn
	FUNC_GPIO_ReadIn gint;	// GINT 中断
	uint8_t readAddr;            // SPI(未用) / I2C 读地址
	uint8_t writeAddr;           // SPI(未用) / I2C 写地址
} driver_sh2100_t;

/// 写单个寄存器
std_err_t sh2100_write_reg(driver_sh2100_t *dev, uint8_t regAddr, uint8_t ch);
/// 读单个寄存器
std_err_t sh2100_read_reg(driver_sh2100_t *dev, uint8_t regAddr, uint8_t *pch);
/// 连续写多个寄存器
std_err_t sh2100_write_regs(driver_sh2100_t *dev, uint8_t regAddr, const uint8_t* buf, uint16_t len);
/// 连续读多个寄存器
std_err_t sh2100_read_regs(driver_sh2100_t *dev, uint8_t regAddr, uint8_t* buf, uint16_t len);

/// 检测芯片是否存在
bool sh2100_chip_exists(driver_sh2100_t *dev);
/// 读加速度传感器xyz方向值
std_err_t sh2100_read_acc_xyz(driver_sh2100_t *dev, s16 xyz[3]);
/// 读陀螺仪xyz方向值
std_err_t sh2100_read_gyr_xyz(driver_sh2100_t *dev, s16 xyz[3]);
/// 打印输出 ACC 
void sh2100_dump_acc_fifo(driver_sh2100_t *dev);

/// 休眠
static inline void sh2100_sleep(driver_sh2100_t *dev) {  }

/// 测试
void sh2100_test(driver_sh2100_t *dev);


#endif // _DRIVER_SH2100_H_
